package cn.tmall.service.impl;

import cn.tmall.mapper.CategoryMapper;
import cn.tmall.mapper.ProductImageMapper;
import cn.tmall.mapper.ProductMapper;
import cn.tmall.pojo.Category;
import cn.tmall.pojo.Product;
import cn.tmall.pojo.ProductExample;
import cn.tmall.pojo.ProductImage;
import cn.tmall.service.OrderItemService;
import cn.tmall.service.ProductImageService;
import cn.tmall.service.ProductService;
import cn.tmall.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("ProductServiceImpl")
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Resource(name = "ProductImageServiceImpl")
    ProductImageService productImageService;
    @Resource(name = "OrderItemServiceImpl")
    OrderItemService orderItemService;
    @Resource(name = "ReviewServiceImpl")
    ReviewService reviewService;

    @Override
    public void add(Product product) {
        productMapper.insert(product);
    }

    @Override
    public void delete(int id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Product product) {
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public Product get(int id) {
        Product product = productMapper.selectByPrimaryKey(id);
        setCategory(product);
        setFirstProductImage(product);
        return product;
    }

    @Override
    public List<Product> list(int cid) {
        ProductExample example = new ProductExample();
        example.createCriteria().andCidEqualTo(cid);
        example.setOrderByClause("id desc");

        List result = productMapper.selectByExample(example);
        setCategory(result);
        setFirstProductImage(result);
        return result;
    }

    @Override
    public void setFirstProductImage(Product product) {
        List<ProductImage> productImageList = productImageService.list(product.getId(), ProductImageService.type_single);
        if (!productImageList.isEmpty()) {
            product.setFirstProductImage(productImageList.get(0));
        }
    }

    @Override
    public void fill(Category category) {
        category.setProducts(this.list(category.getId()));
    }

    @Override
    public void fill(List<Category> categoryList) {
        for (Category category : categoryList) {
            this.fill(category);
        }
    }

    @Override
    public void fillByRow(List<Category> categoryList) {
        int productNumberEachRow = 8;
        for (Category category : categoryList) {
            List<Product> products = category.getProducts();
            List<List<Product>> productsByRow = new ArrayList<>();
            for (int i = 0; i < products.size(); i += productNumberEachRow) {
                int size = i + productNumberEachRow;
                size = size > products.size() ? products.size() : size;
                List<Product> productList = products.subList(i, size);
                productsByRow.add(productList);
            }
            category.setProductsByRow(productsByRow);
        }
    }

    @Override
    public void setSaleAndReviewNumber(Product product) {
        int saleCount = orderItemService.getSaleCount(product.getId());
        product.setSaleCount(saleCount);   //给产品设置月销量

        int reviewCount = reviewService.getReviewCount(product.getId());
        product.setReviewCount(reviewCount);  //给产品设置评价数
    }

    @Override
    public void setSaleAndReviewNumber(List<Product> productList) {
        for (Product product : productList) {
            this.setSaleAndReviewNumber(product);
        }
    }

    @Override
    public List<Product> search(String keyword) {
        ProductExample example = new ProductExample();
        example.createCriteria().andNameLike("%" + keyword + "%");
        example.setOrderByClause("id desc");
        List<Product> productList = productMapper.selectByExample(example);
        this.setFirstProductImage(productList);
        this.setCategory(productList);
        return productList;
    }

    public void setFirstProductImage(List<Product> productList) {
        for (Product product : productList) {
            List<ProductImage> productImageList = productImageService.list(product.getId(), ProductImageService.type_single);
            if (!productImageList.isEmpty()) {
                product.setFirstProductImage(productImageList.get(0));
            }
        }
    }
    //以下两个方法用于给Product自定义的Category字段赋值
    public void setCategory(Product product) {
        int cid = product.getCid();
        Category category = categoryMapper.get(cid);
        product.setCategory(category);
    }
    public void setCategory(List<Product> productList) {
        for (Product product : productList) {
            setCategory(product);
        }
    }
}
